package com.hongdee.scm.outwarehouse.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.outwarehouse.service.ITScmOutWareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/5/16.
 */
@RestController
@RequestMapping("/scm/out")
public class TScmOutWareHouseController {
    @Autowired
    private ITScmOutWareHouseService itScmOutWareHouseService;

    /**
     * 出库单分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itScmOutWareHouseService.page(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 确认出库
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResultEntity update(@RequestParam(name = "uid", defaultValue = "0") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestBody List<Map<String,Object>> data) throws Exception {

        //type:1销售2调拨
        String  status="";
        String str = "";

        for(Map<String,Object> map:data){
            str = this.itScmOutWareHouseService.confirmOut(map,uid);
            if("diaobo".equals(str)) { //调拨出库
                //修改车辆状态（3调拨在途）
                status = "3";
                this.itScmOutWareHouseService.updateCarOutStatus(map,status,uid);
            }else if("xiaoshou".equals(str)){//销售出库
                //修改车辆状态（2已售）
                status = "2";
                this.itScmOutWareHouseService.updateCarOutStatus(map,status,uid);
            }else{
                throw new Exception("出库失败");
            }
        }
        ResultEntity res = new ResultEntity();
        res.setMsg("车辆已出库！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 未出库车辆
     *
     * @param uid
     * @param token
     * @param type
     * @return
     * @throws Exception
     */
    @RequestMapping("/notOut")
    @ResponseBody
    public List notOutWareHouse(@RequestParam(name = "uid", defaultValue = "0") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "type", defaultValue = "") String type) throws Exception {
        return this.itScmOutWareHouseService.notOutWareHouse(type, uid);
    }

    /**
     * 生成出库单(销售)
     *
     * @param uid
     * @param token
     * @param type
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "0") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "type", defaultValue = "") String type,
                             @RequestBody Map data) throws Exception {
        //type1销售“”调拨
        this.itScmOutWareHouseService.prepareOut(data,uid,type);
        //this.itScmOutWareHouseService.outWareHouse(data, uid, type);
        ResultEntity res = new ResultEntity();
        res.setMsg("生成出库单成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 调拨出库单保存
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveRequisition")
    @ResponseBody
    public ResultEntity saveRequisition(@RequestParam(name = "uid", defaultValue = "0") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "type", defaultValue = "") String type,
                             @RequestBody Map data) throws Exception {
        //this.itScmOutWareHouseService.requisitionSaveOrUpdate(data, uid);
        this.itScmOutWareHouseService.prepareOut(data,uid,type);
        List list = new ArrayList();
        if(type.equals("2")){
            list = this.itScmOutWareHouseService.updateSerialNum(data.get("relationOrderNum").toString(),uid);
        }
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setDataList(list);
        return res;
    }

    /**
     * 调拨出库分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/requisitionPage")
    @ResponseBody
    public Map requisitionPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "start", defaultValue = "0") String start,
                               @RequestParam(name = "limit", defaultValue = "50") String limit,
                               @RequestBody Map data) throws Exception {

        Map map = this.itScmOutWareHouseService.requisitionPage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 销售出库分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/salePage")
    @ResponseBody
    public Map salePage(@RequestParam(name = "uid", defaultValue = "") String uid,
                        @RequestParam(name = "token", defaultValue = "") String token,
                        @RequestParam(name = "start", defaultValue = "0") String start,
                        @RequestParam(name = "limit", defaultValue = "50") String limit,
                        @RequestBody Map data) throws Exception {

        Map map = this.itScmOutWareHouseService.salePage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

}
